<!DOCTYPE html>
<body>
    <head>
        <meta charset="utf-8">
        <title>自定义对话框预览</title>
        <#include "/pubHeadStyleTree.html">
        <script src="${ctx}/formStatic/form/CommonDialog.js"></script>
    </head>
    <body>
        <input type="hidden" name="selectData">
        <input type="hidden" name="selectDataIds">
        <div class="ccpui-tablesize" style="padding:10px;position: relative">
            <table id="formDialog" lay-filter="formDialog"></table>
        </div>
        <#if bpmFormDialog.style==1>
        <div id="dialogTree" class="ztree"></div>

        <#else>
        <script type="text/html" id="formDialogToolsBar">
            <div class="layui-btn-container">
                <a class="layui-btn layui-btn-sm" lay-event="query">
                    <i class="layui-icon layui-icon-search"></i>
                    <span>查询</span>
                </a>

            </div>
            <form class="layui-form" action="" style="margin-top:10px" lay-filter="formDialog_query" onsubmit="return false">
                <div class="table-top-filter">
                    <#if bpmFormDialog.conditionList?? && (bpmFormDialog.conditionList?size > 0) >
                    <#list bpmFormDialog.conditionList as item>
                    <#if item.defaultType==1>
                    <div class="table-filter-alone">
                        <label class="layui-form-label outofHiding" title="${item.comment }">${item.comment }</label>
                        <#if item.fieldType=='date'>
                        <#if item.condition=='='>
                        <input type="text" name="Q_${item.fieldName}_DL" class="layui-input start_time filter-value"/>
                        <#elseif item.condition=='>='>
                        <input type="text" name="Q_start${item.fieldName}_DL" class="layui-input start_time filter-value"/>
                        <#elseif item.condition=='<='>
                        <input type="text" name="Q_end${item.fieldName}_DG" class="layui-input end_time filter-value"/>
                        <#else>
                        <input type="text" name="Q_start${item.fieldName}_DL"
                               class="layui-input filter-value start_time"/>
                        <label class="layui-form-label">到:</label>
                        <input type="text" name="Q_end${item.fieldName}_DG" class="layui-input filter-value end_time"/>
                    </#if>
                    <#elseif item.fieldType=='number'>
                    <input type="text" name="Q_${item.fieldName}_DB" class="layui-input"/>
                    <#else>
                    <#if item.paraCt=='1'>
                    <input type="text" name="${item.fieldName }" class="layui-input filter-value"/>
                </#if>
            </#if>
            <#if item.fieldType!='date'>
            <#if item.paraCt=='0'>
            <input type="hidden" name="selectIds">
            <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
            <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectCustomDialog(this);"id="dialog" dialog="${item.dialog}" param="${item.param}" style="margin: 4px;width:48px;">
            <#elseif item.paraCt=='4'>
            <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
            <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectUser(this);" style="margin: 4px;width:48px;">
            <#elseif item.paraCt=='17'>
            <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
            <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectRole(this);" style="margin: 4px;width:48px;">
            <#elseif item.paraCt=='18'>
            <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
            <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectOrg(this);" style="margin: 4px;width:48px;">
            <#elseif item.paraCt=='19'> <!--岗位-->
            <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
            <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectUser(this);" style="margin: 4px;width:48px;">
            </#if>
            </div>
            </#if> </#if>
            </#list>
            </#if>
            <input type="hidden" name="dialog_alias_" value="${bpmFormDialog.alias}">
            </div>
            </form>
        </script>
    </#if>
    <textarea id="txtJsonData" style="display:none"></textarea>
    <input style="display:none" id="parentIndex"/>
    <script>
        var needPage = true;
        if ('${bpmFormDialog.needpage}' == 0) {
            needPage = false;
        }
        var setter;
        var isOpenJsonData = '${isOpenJsonData}';
        layui.config({
            base: serverPath.formPath + staticPath.formPath //静态资源所在路径
        }).extend({
             index: 'index' //主入口模块
        }).use(['index', 'commons','table', 'layer', 'jquery', 'form', 'tree', 'common', 'totalFilter', 'setter', 'code'], function () {
            var table = layui.table, layer = layui.layer, commons = layui.commons,
                common = layui.common, totalFilter = layui.totalFilter, form = layui.form, code = layui.code;
            common.tableSort("formDialog");
            layui.code();
            setter = layui.setter;

            var selList = [];
            if(layui.selList) {
                selList.push.apply(selList,layui.selList);
            }
            if ('${bpmFormDialog.style}' ==='0') {
                //组装列表显示字段数据
                var displayField = JSON.parse('${bpmFormDialog.displayfield}');
                var sortfield = JSON.parse('${bpmFormDialog.sortfield}');
                var isSingle = '${bpmFormDialog.issingle}';
                var array = [];
                var checkboxField = {type: 'checkbox', fixed: 'left'};
                if (isSingle === '1') {
                    checkboxField = {type: 'radio', fixed: 'left'};
                }
                array.push(checkboxField);
                for (var i = 0; i < displayField.length; i++) {
                    var json = {};
                    var fieldName = displayField[i].field;
                    json.field = fieldName;
                    json.title = displayField[i].comment;
                    json.align = "center";
                    for (var k = 0; k < sortfield.length; k++) {
                        if (sortfield[k].field == fieldName){
                            json.sort = true;
                        }
                    }
                    array.push(json);
                }

                //渲染表格
                table.render({
                    elem: '#formDialog'
                    , cellMinWidth: 80
                    , title: '自定义对话框表'
                    , defaultToolbar: []
                    , height: 'full-75'
                    , url: serverPath.formPath + setter.formLinkPath + 'formDialog/showTableData?alias=' + '${alias}'+'${paramStr}'
                    , toolbar: '#formDialogToolsBar'
                    , page: {limit: '${bpmFormDialog.pagesize}', page: needPage}
                    , limits: [5, 10, 15, 20, 50]
                    , limit: '${bpmFormDialog.pagesize}'
                    , autoSort: false
                    , cols: [array]
                    ,parseData: function (res) {
                        var _data = res.data;
                        //获取主键
                        var count=0;
                        var pkStr="ID";
                        for(var p in _data[0]){
                            count++;
                            var str = _data[0][p];
                            if(str != null && str != ""){
                                if(count == 1 && (str.length == 32 || str=='orgResourcesManager') && p.indexOf("ID")>0){
                                    pkStr=p;
                                    break;
                                }
                            }
                        }
                        if (_data.length > 0 && selList.length > 0) {
                            for (var i = 0; i < _data.length; i++) {
                                var _d = _data[i];
                                for (var j = 0; j < selList.length; j++) {
                                    var _td = selList[j];
                                    if (_d[pkStr] === _td.ID) {
                                        _d.LAY_CHECKED = true;
                                    }
                                }
                            }
                        }
                    }
                });
                totalFilter.init('formDialog');


                table.on('toolbar(formDialog)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data; //获取选中行数据
                    switch (obj.event) {
                        case 'query':
                            common.tableConditionRender("formDialog", "formDialog_query", {where: {}}, false,false,'${bpmFormDialog.pagesize}');
                            commons.reflashDate({startName: '.start_time', endName: '.end_time'});
                            break;
                    }
                });

                window.Initialization = function () {
                    commons.reflashDate({startName: '.start_time', endName: '.end_time'});
                };
                /*刷新时间控件*/
                commons.reflashDate({startName: '.start_time', endName: '.end_time'});
                common.tableSort("formDialog", {show: window.Initialization});

                //获取选中数量
                function getRadioCheckCount(){
                    var count=0;
                    var isSingle = '${bpmFormDialog.issingle}';
                    if(isSingle === '1') {
                        var radios= $("input[lay-type='layTableRadio']");
                        radios.each(function () {
                            if ($(this).prop("checked") === true) {
                                count++
                            }
                        });
                    }else{
                        var item =  $("input[name='layTableCheckbox']");
                        item.each(function () {
                            if ($(this).prop("checked") === true) {
                                count++
                            }
                        });
                    }
                    return count;
                }
            }

            //树的处理开始====
            var zTree;
            var json=JSON.parse('${bpmFormDialog.displayfield}');
            var isSingle = '${bpmFormDialog.issingle}';
            var radioCheckboxFlag = false;
            if (isSingle === '0') {
                radioCheckboxFlag = true;
            }
            if ('${bpmFormDialog.style}' === '1') {
                loadTree();
            }
            //加载树
            function loadTree() {
                var setting = {
                    async:{
                        enable:true,
                        url:serverPath.formPath + setter.formLinkPath + 'formDialog/getTreeData?alias='+'${alias}'+'&isRoot=1',
                        autoParam:[json.id+"=idKey",json.pid+"=pidKey",json.displayName+"=nameKey"],
                        otherParam:{idKeyName:json.id,pidKeyName:json.pid,nameKeyName:json.displayName}
                    },
                    data: {
                        key : {name: json.displayName},
                        simpleData: {
                            enable: true,
                            idKey: json.id,
                            pIdKey: json.pid
                        }
                    },
                    check: {
                        enable: radioCheckboxFlag
                    },
                    callback: {
                        onClick: getClickData
                    }
                };
                zTree=$.fn.zTree.init($('#dialogTree'), setting);
            }
            //取消
            $("#cancel").click(function(){
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });
            //重置
            $("#reset").click(function(){
                zTree.checkAllNodes(false);
            });

            //返回字段值
            var resultField = JSON.parse('${bpmFormDialog.resultfield}');
            var resultSingleJson={};
            //树节点单击事件
            function getClickData(event, treeId, treeNode) {
                for(var j=0;j<resultField.length;j++){
                    var returnField = resultField[j].field;
                    resultSingleJson[returnField]=treeNode[returnField];
                }
                $("#txtJsonData").val(JSON.stringify(resultSingleJson));
            }

            //打开json数据弹框
            function openQueryJsonDialog(){
                parent.layer.open({
                    type: 2,
                    area: ["550px",'375px'],
                    title: ['提示'],
                    content: serverPath.formPath + setter.formLinkPath + "formDialog/view/showJson?parentName="+window.name,
                    success: function (layero) {
                        var mask = $(".layui-layer-shade");
                        mask.appendTo(layero.parent());
                    }
                });
            }

            //确定事件
            window.determineQuery=function(){
                if ('${bpmFormDialog.style}' === '1') {
                    if(radioCheckboxFlag){ //多选
                        var returnData=[];
                        var treeObj=$.fn.zTree.getZTreeObj("dialogTree"),
                            nodes=treeObj.getCheckedNodes(true);
                        for(var i=0;i<nodes.length;i++){
                            if(!nodes[i].getCheckStatus().half){
                                var resultJson={};
                                for(var j=0;j<resultField.length;j++){
                                    var returnField = resultField[j].field;
                                    resultJson[returnField]=nodes[i][returnField];
                                }
                                returnData.push(resultJson);
                            }
                        }
                        if(JSON.stringify(returnData)==="[]") {
                            layer.alert("还没有选择数据项！");
                            return false;
                        }
                        $("#txtJsonData").val(JSON.stringify(returnData));
                    }else{
                        if(JSON.stringify(resultSingleJson)==="{}") {
                            layer.alert("还没有选择数据项！");
                            return false;
                        }
                    }
                    $("input[name='selectData']").val($("#txtJsonData").val());
                    if(isOpenJsonData){
                        openQueryJsonDialog();
                    }
                    $("#parentIndex").val(parent.layer.getFrameIndex(window.name));
                } else {
                    var checkStatus = table.checkStatus('formDialog'), rowData = checkStatus.data;
                    if (getRadioCheckCount() <= 0) {
                        layer.alert("还没有选择数据项！");
                        return false;
                    }
                    $("#parentIndex").val(parent.layer.getFrameIndex(window.name));
                    var isSingle = '${bpmFormDialog.issingle}';
                    var resultField = JSON.parse('${bpmFormDialog.resultfield}');
                    //获取主键
                    var count=0;
                    var pkStr="ID";
                    for(var p in rowData[0]){
                        count++;
                        if(count == 1 && (rowData[0][p].length == 32 || rowData[0][p]=='orgResourcesManager') && p.indexOf("ID")>0){
                            pkStr = p;
                            break;
                        }
                    }
                    if (isSingle === '0') { //多选
                        var returnData = [];
                        var ids=[];
                        for (var k = 0; k < rowData.length; k++) {
                            ids.push(rowData[k][pkStr]);
                            var resultJson = {};
                            for (var j = 0; j < resultField.length; j++) {
                                var returnField = resultField[j].field;
                                resultJson[returnField] = rowData[k][returnField];
                                if (j == resultField.length - 1) {
                                    returnData.push(resultJson);
                                }
                            }
                        }
                        $("#txtJsonData").val(JSON.stringify(returnData));
                        $("input[name='selectDataIds']").val(ids);
                    } else {
                        var resultJson = {};
                        for (var p = 0; p < resultField.length; p++) {
                            resultJson[resultField[p].field] = rowData[0][resultField[p].field];
                        }
                        $("#txtJsonData").val(JSON.stringify(resultJson));
                        $("input[name='selectDataIds']").val(rowData[0][pkStr]);
                    }
                    $("input[name='selectData']").val($("#txtJsonData").val());
                    if (isOpenJsonData) {
                        openQueryJsonDialog();
                    }
                }
            };
        });
    </script>
    <style>
        .layui-table-tool {
            position: inherit;
        }
        #LAY_app{
            position: relative;
        }
        .outofHiding{
            width: 141px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
    </style>
</body>
</html>
